package com.gykfbxxh.imovie.custom.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.util.ViewSizeUtils;
import com.gykfbxxh.imovie.R;

public class SimpleViewPagerIndicator extends LinearLayout {

    private static final int COLOR_TEXT_NORMAL = 0xFF606060;
    private static final int COLOR_TEXT_SELECTOR = 0xFF161616;
    private static final int COLOR_TEXT_LINE = 0xFFF9D13F;
    private static final int DEFAULT_TEXTPADBOTTOM = 0;

    private String[] mTitles;
    private int mTabCount;
    private int mIndicatorColor;
    private float mTranslationX;
    private Paint mPaint = new Paint();
    private int mTabWidth;
    private int mLeftWidth;
    private int mTextSize = 18;
    private int mPadBottom;
    private boolean isPaddingLeft = true;

    public SimpleViewPagerIndicator(Context context) {
        this(context, null);
    }

    public SimpleViewPagerIndicator(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SimpleViewPagerIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleIndicator, 0, 0);
        mPadBottom = a.getDimensionPixelSize(R.styleable.SimpleIndicator_text_padding_bottom, DEFAULT_TEXTPADBOTTOM);
        mIndicatorColor = a.getColor(R.styleable.SimpleIndicator_text_sel_color, COLOR_TEXT_SELECTOR);
        a.recycle();

        mPaint.setColor(COLOR_TEXT_LINE);
        mPaint.setStrokeWidth(ViewSizeUtils.dp2px(getContext(), 2f));
    }

    public void setStrokeWidth(int width) {
        mPaint.setStrokeWidth(width);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mTitles != null && mTitles.length != 0) {
            if(isPaddingLeft){
                mTabWidth = ViewSizeUtils.dp2px(getContext(), mTitles[0].length() * 18 -8);
            } else {
                mTabWidth = ViewSizeUtils.getWidthPixels()/mTabCount;
            }
        }
        mLeftWidth = (w / mTabCount - mTabWidth) / 2;
    }

    /**
     * 在setTitles之前调用
     * @param mTextSize
     */
    public void setmTextSize(int mTextSize) {
        this.mTextSize = mTextSize;
    }

    public void setTitles(String[] titles) {
        mTitles = titles;
        mTabCount = titles.length;
        generateTitleView();

    }

    public void setIndicatorColor(int indicatorColor) {
        this.mIndicatorColor = indicatorColor;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        canvas.translate(mTranslationX, getHeight() - 2);
        canvas.drawLine(mLeftWidth, 0, mLeftWidth + mTabWidth, 0, mPaint);
//        canvas.drawRoundRect(mLeftWidth, 0, mLeftWidth + mTabWidth, 0, ,,mPaint);
        canvas.restore();
    }

    public void scroll(int position, float offset) {
        /**
         *  0-1:position=0 ;1-0:postion=0;
         */
        mTranslationX = getWidth() / mTabCount * (position + offset);
        invalidate();
    }

    public void pageSelect(int position) {
        if (position >= getChildCount()) {
            return;
        }
        for (int i = 0; i < getChildCount(); i++) {
            TextView textView = (TextView) getChildAt(i);
            if (i == position) {
                textView.setTextColor(mIndicatorColor);
            } else {
                textView.setTextColor(COLOR_TEXT_NORMAL);
            }
        }
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    private void generateTitleView() {
        if (getChildCount() > 0)
            this.removeAllViews();
        int count = mTitles.length;

        setWeightSum(count);
        for (int i = 0; i < count; i++) {
            TextView tv = new TextView(getContext());
            LayoutParams lp = new LayoutParams(0,
                    LayoutParams.MATCH_PARENT);
            lp.weight = 1;
            tv.setGravity(Gravity.CENTER);
            tv.setTextColor(COLOR_TEXT_NORMAL);
            tv.setPadding(5, 5, 5, mPadBottom);
            tv.setText(mTitles[i]);
            tv.setTag(i);
            tv.setTextSize(mTextSize);
            tv.setLayoutParams(lp);
            tv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    pageSelect((Integer) v.getTag());
                    if (onIndicatorTitleClick != null) {
                        onIndicatorTitleClick.onClick((Integer) v.getTag());
                    }
                }
            });
            addView(tv);
        }
    }

    public void updataTitle(String str, int position) {
        if (getChildCount() <= position) {
            return;
        }
        TextView textView = (TextView) getChildAt(position);
        textView.setText(str);
    }

    private IndicatorTitleClick onIndicatorTitleClick;

    public IndicatorTitleClick getOnIndicatorTitleClick() {
        return onIndicatorTitleClick;
    }

    public void setOnIndicatorTitleClick(IndicatorTitleClick onIndicatorTitleClick) {
        this.onIndicatorTitleClick = onIndicatorTitleClick;
    }

    public interface IndicatorTitleClick

    {
        public void onClick(int position);
    }

    public boolean isPaddingLeft() {
        return isPaddingLeft;
    }

    public void setPaddingLeft(boolean paddingLeft) {
        isPaddingLeft = paddingLeft;
    }
}
